package qc.module.demo.api.controller;

import com.rtdb.api.model.RtdbData;
import com.rtdb.service.impl.HistorianImpl;
import com.rtdb.service.impl.ServerImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import qc.common.core.utils.DateUtil;
import qc.module.demo.dto.student.ScoreDto;
import qc.module.demo.entity.HandleScore;
import qc.module.demo.entity.OriginalScore;
import qc.module.demo.service.ScoreService;
import qc.module.demo.service.StudentService;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/student")
public class DemoController {

    @Autowired
    private StudentService studentService;

    @Autowired
    private ScoreService scoreService;

    /**
     * 根据学号查询学生成绩
     *
     * @param no 学号
     * @return 学生成绩集合
     * @author cgy
     * @since 2023/6/20
     */
    @RequestMapping(value = "/score/{no}", method = {RequestMethod.GET})
    public List<ScoreDto> getScore(@PathVariable Integer no) {
        return studentService.getScore(no);
    }

    /**
     * 保存原始成绩数据
     *
     * @param originalScore
     * @return
     */
    @RequestMapping(value = "/addOriginalScore", method = {RequestMethod.POST})
    public String addOriginalScore(@RequestBody OriginalScore originalScore) {
        return scoreService.addOriginalScore(originalScore);
    }

    /**
     * 保存处理好的数据
     *
     * @param handleScore
     * @return
     */
    @RequestMapping(value = "/addHandleScore", method = {RequestMethod.POST})
    public String addHandleScore(@RequestBody HandleScore handleScore) {
        return scoreService.addHandleScore(handleScore);
    }

    /**
     * 测试内存溢出
     * @param args
     * @throws InterruptedException
     */
    public static void main(String[] args) throws Exception {
        //int i = 0;
        //List<ByteBuffer> buffers = new ArrayList<>();
        //while (true){
        //    ByteBuffer bb = ByteBuffer.allocateDirect(1024 * 1024 * 1);
        //    buffers.add(bb);
        //    Thread.sleep(1000);
        //    System.out.println(i++);
        //}

        //获取单个数据库连接
        ServerImpl server = new ServerImpl("127.0.0.1",6327,"sa","VeStore-123#!");
        System.out.println("server : " + server);

        //BaseImpl base = new BaseImpl(server);
        //添加新表
        //base.appendTable("test1","ces1");

        //根据 "表名.标签点名" 格式批量获取标签点标识
        //List<MinPoint> points = base.findPoints(new String[]{"test.value","test.ID"});
        
        //根据标签点ids批量获取最后一次被修改的时间
        //String[] exchDates = base.getChangeDates(new int[]{239,242});

        //根据表名称获取表中包含的标签点数量
        //int count = base.getTableSizeByName("test");


        HistorianImpl historian = new HistorianImpl(server);
        
        Date start = DateUtil.parseDate("2024-09-03 00:00:00");
        Date end = DateUtil.parseDate("2024-09-03 23:59:59");
        //获取单个标签点在一段时间范围内的存储值数量
        //int valueCount = historian.archivedValuesCount(242,start,end);
        //System.out.println("获取单个标签点在一段时间范围内的存储值数量：" + valueCount);

        //获取单个标签点在一段时间范围内的真实的存储值数量
        //int realvalueCount = historian.archivedValuesRealCount(242,start,end);
        //System.out.println("获取单个标签点在一段时间范围内的真实的存储值数量：" + realvalueCount);
        
        //读取单个标签点一段时间内的储存数据，参数：标签点id，获取个数，开始时间，结束时间   手册418页
        List<RtdbData> archivedValues = historian.getArchivedValues(249, 10, start, end);
        for (RtdbData data : archivedValues){
            System.out.println("获取到的数据：" + data.getValue());
        }

        //关闭连接
        server.close();
    }
}
